package cn.bugstack.mybatis.mapping;

import cn.bugstack.mybatis.session.Configuration;
import cn.bugstack.mybatis.type.JdbcType;
import cn.bugstack.mybatis.type.TypeHandler;
import cn.bugstack.mybatis.type.TypeHandlerRegistry;

import java.util.List;

/**
 * @author wcc
 * @description 结果映射 resultMap中字段的映射
 * @create
 */

public class ResultMapping {

    private Configuration configuration;
    private String property;
    private String column;
    private Class<?> javaType;
    private TypeHandler<?> typeHandler;
    private List<ResultFlag> flags;

    ResultMapping() {

    }

    public static class Builder {
        private ResultMapping resultMapping = new ResultMapping();
        public Builder(Configuration configuration, String property, String column, Class<?> javaType){
            resultMapping.configuration = configuration;
            resultMapping.property = property;
            resultMapping.column = column;
            resultMapping.javaType = javaType;
        }
        public Builder typeHandler(TypeHandler<?> typeHandler){
            resultMapping.typeHandler = typeHandler;
            if (typeHandler == null){
                resolveTypeHandler();
            }
            return this;
        }

        private void resolveTypeHandler(){
            Configuration configuration = resultMapping.configuration;
            TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();
            resultMapping.typeHandler = typeHandlerRegistry.getTypeHandler(resultMapping.javaType,null);
        }

        public Builder flags(List<ResultFlag> flags){
            resultMapping.flags = flags;
            return this;
        }
        public ResultMapping build(){
            return resultMapping;
        }
    }
    public Configuration getConfiguration() {
        return configuration;
    }

    public String getProperty() {
        return property;
    }

    public String getColumn() {
        return column;
    }

    public Class<?> getJavaType() {
        return javaType;
    }

    public TypeHandler<?> getTypeHandler() {
        return typeHandler;
    }

    public List<ResultFlag> getFlags() {
        return flags;
    }


}

